/*** Program Compute_PPD_checks_TFP_dispersion.sas.

     This program uses the within-industry-year TFP IQRs
     for ready-mix concrete to conducts validity checks of the 
     CART imputations..

***/

/* OPTIONS OBS=100 NOREPLACE  ; */

%include "../../ASMimplibs.sas"; 


************;
*  MACROS  *;
************;



%MACRO get_TFP_IQR_ratios(data=,year=);


 data TFP&data.&year._iqrs (keep = _IMPUTATION_ tfp7525ratio&data);
  set concrete.concrete_resids_&data.&year._iqrs;
  tfp7525ratio&data = exp(ltfp_iqr_cs_resid);
 run;

%MEND;



%MACRO compute_p_value(year=,Mfirst=,Mlast=);

  data pairs; 
  merge TFPCART&year._iqrs TFPpred&year._iqrs; 
  by _IMPUTATION_; 
  if tfp7525ratiopred > tfp7525ratioCART then I=1; 
  else I=0;
  tfp7525ratio_diff = tfp7525ratiopred - tfp7525ratioCART;   
  run;

  proc means data=pairs N p10 q1 median mean q3 p90;
   var tfp7525ratiopred tfp7525ratioCART tfp7525ratio_diff;
   title1 "Distribution of concrete TFP 75-25 ratio difference, &year";
   title2 "CART-predicted est. minus CART-completed est.";
  run;

  proc means data=pairs mean noprint;
   var I;
  output out=pval mean(I)= indicator_mean;
  run;


  data pval;
   set pval;
    if indicator_mean < 0.5 then pval = 2*indicator_mean;
    else pval = 2*(1-indicator_mean);
  run;

  proc print data=pval;
   var pval;
  title1 "Synthetic P-value for concrete imputation validity check";
  title2 "Test of whether TFP 75-25 ratio in &year";
  title3 "using predicted data consistently differs from estimate from";
  title4 "completed data in one direction, 500 pairs of datasets";

%MEND;


******************;
* START PROGRAM  *;
******************;


%get_TFP_IQR_ratios(data=CART,year=02);
*** Input dataset(s):  concrete.concrete_resids_CART02_iqrs
*** Output dataset(s): TFPCART02_iqrs;


%get_TFP_IQR_ratios(data=CART,year=07);
*** Input dataset(s):  concrete.concrete_resids_CART07_iqrs
*** Output dataset(s): TFPCART07_iqrs;


/** Compute the same TFP 75-25 ratios using the predicted data. */

%get_TFP_IQR_ratios(data=pred,year=02);
*** Input dataset(s):  concrete.concrete_resids_CART02_iqrs
*** Output dataset(s): TFPpred02_iqrs;

%get_TFP_IQR_ratios(data=pred,year=07);
*** Input dataset(s):  concrete.concrete_resids_CART07_iqrs
*** Output dataset(s): TFPpred07_iqrs;



%compute_p_value(year=02,Mfirst=1,Mlast=500);
%compute_p_value(year=07,Mfirst=1,Mlast=500);

